<template>
  <div class="app-container">

    <el-row :gutter="20">
      <el-col :span="12">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="字典名称" prop="dictName">
            <el-input
              v-model="queryParams.dictName"
              placeholder="请输入字典名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="字典类型" prop="dictType">
            <el-input
              v-model="queryParams.dictType"
              placeholder="请输入字典类型"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
            >删除</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="typeList"  @current-change="handleSelectionChange"  @selection-change="handleSelectionZChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="字典编号" align="center" prop="dictId" />
          <el-table-column label="字典名称" align="center" prop="dictName" :show-overflow-tooltip="true" />
          <el-table-column label="字典类型" align="center" prop="dictType" :show-overflow-tooltip="true"/>
          <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
          <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination  @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10,20,50]" :page-size="pageSize" :current-page="currentPage" :total="pageTotal" background></el-pagination>
      </el-col>
      <el-col :span="12">
        <el-form :model="dqueryParams" ref="dqueryForm" :inline="true" v-show="dshowSearch" label-width="68px">
          <el-form-item label="字典标签" prop="dictLabel">
            <el-input
              v-model="dqueryParams.dictLabel"
              placeholder="请输入字典标签"
              clearable
              size="small"
              @keyup.enter.native="dhandleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="dhandleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="dresetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="dhandleAdd"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="dsingle"
              @click="dhandleUpdate"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="dmultiple"
              @click="dhandleDelete"
            >删除</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="dloading" :data="dataList" @selection-change="dhandleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="字典编码" align="center" prop="dictCode" />
          <el-table-column label="字典标签" align="center" prop="dictLabel" />
          <el-table-column label="字典键值" align="center" prop="dictValue" />
          <el-table-column label="字典排序" align="center" prop="dictSort" />
          <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
          <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="dhandleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="dhandleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination  @size-change="dhandleSizeChange" @current-change="dhandleCurrentChange" :page-sizes="[10,20,50]" :page-size="dpageSize" :current-page="dcurrentPage" :total="dpageTotal" background></el-pagination>
      </el-col>
    </el-row>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="form.dictName" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="form.dictType" placeholder="请输入字典类型" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="dopen" width="500px" append-to-body>
      <el-form ref="dform" :model="dform" :rules="rules" label-width="80px">
        <el-form-item label="字典类型">
          <el-input v-model="dform.dictType" :disabled="true" />
        </el-form-item>
        <el-form-item label="数据标签" prop="dictLabel">
          <el-input v-model="dform.dictLabel" placeholder="请输入数据标签" />
        </el-form-item>
        <el-form-item label="数据键值" prop="dictValue">
          <el-input v-model="dform.dictValue" placeholder="请输入数据键值" />
        </el-form-item>
        <el-form-item label="显示排序" prop="dictSort">
          <el-input-number v-model="dform.dictSort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="dform.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="dform.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dsubmitForm">确 定</el-button>
        <el-button @click="dcancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listType, getType, delType, addType, updateType, exportType, clearCache } from "@/api/dict";
import { listData, getData, delData, addData, updateData, exportData } from "@/api/data";

export default {
  name: "Dict",
  data() {
    return {
      // 遮罩层
      loading: false,
      dloading: false,
      pageSize: 10,
      currentPage: 1,
      pageTotal: 0,
      pageNo: 1,
      dpageSize: 10,
      dcurrentPage: 1,
      dpageTotal: 0,
      dpageNo: 1,
      currentType: {},
      // 选中数组
      ids: [],
      dids: [],
      // 非单个禁用
      single: true,
      dsingle: true,
      // 非多个禁用
      multiple: true,
      dmultiple: true,
      // 显示搜索条件
      showSearch: true,
      dshowSearch: true,
      // 总条数
      total: 0,
      dtotal: 0,
      // 字典表格数据
      typeList: [],
      // 字典表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dopen: false,
      // 状态数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dictName: undefined,
        dictType: undefined,
        status: undefined
      },
      // 查询参数
      dqueryParams: {
        pageNum: 1,
        pageSize: 10,
        dictName: undefined,
        dictType: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      dform: {},
      // 表单校验
      rules: {
        dictName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" }
        ],
        dictType: [
          { required: true, message: "字典类型不能为空", trigger: "blur" }
        ],
        dictLabel: [
          { required: true, message: "数据标签不能为空", trigger: "blur" }
        ],
        dictValue: [
          { required: true, message: "数据键值不能为空", trigger: "blur" }
        ],
        dictSort: [
          { required: true, message: "数据顺序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data.data;
    });
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      listType(this.queryParams).then(res => {
        this.typeList = res.data.list;
        this.pageSize = res.data.pageSize
        this.pageTotal = res.data.total
        this.currentPage = res.data.pageNum
        this.loading = false;
        }
      );
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.currentPage=val
      this.getList();
    },
    dhandleCurrentChange(val) {
      this.dqueryParams.pageNum = val;
      this.dcurrentPage=val
      this.getDataList();
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.queryParams.pageNum = 1
      this.currentPage=1
      this.getList();
    },
    dhandleSizeChange(val) {
      this.dqueryParams.pageSize = val;
      this.dqueryParams.pageNum = 1
      this.dcurrentPage=1
      this.getDataList();
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    dcancel() {
      this.dopen = false;
      this.dreset();
    },
    // 表单重置
    reset() {
      this.form = {
        dictId: undefined,
        dictName: undefined,
        dictType: undefined,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    // 表单重置
    dreset() {
      this.dform = {
        dictCode: undefined,
        dictLabel: undefined,
        dictValue: undefined,
        dictSort: 0,
        status: "0",
        remark: undefined
      };
      this.resetForm("dform");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索按钮操作 */
    dhandleQuery() {
      this.dqueryParams.pageNum = 1;
      this.getDataList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 重置按钮操作 */
    dresetQuery() {
      this.resetForm("dqueryForm");
      this.dhandleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加字典类型";
    },
    // 多选框选中数据
    handleSelectionChange(value) {
      this.dqueryParams.dictType = value.dictType
      this.search()
    },
    search() {
      this.dqueryParams.pageNum = 1;
      this.getDataList();
    },
    getDataList() {
      this.dloading = true;
      listData(this.dqueryParams).then(response => {
          this.dataList = response.data.list
          this.dloading = false
        this.dpageSize = response.data.pageSize
        this.dpageTotal = response.data.total
        this.dcurrentPage = response.data.pageNum
        }
      );
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dictId = row.dictId || this.ids
      getType(dictId).then(response => {
        this.form = response.data.data;
        this.open = true;
        this.title = "修改字典类型";
      });
    },// 多选框选中数据
    dhandleSelectionChange(selection) {
      this.dids = selection.map(item => item.dictCode)
      this.dsingle = selection.length!=1
      this.dmultiple = !selection.length
    },
    handleSelectionZChange(selection) {
      this.ids = selection.map(item => item.dictId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    dhandleAdd() {
      this.dreset();
      this.dopen = true;
      this.title = "添加字典数据";
      this.dform.dictType = this.dqueryParams.dictType;
    },
    /** 修改按钮操作 */
    dhandleUpdate(row) {
      this.dreset();
      const dictCode = row.dictCode || this.dids
      getData(dictCode).then(response => {
        console.log(response.data.data)
        this.dform = response.data.data;
        this.dopen = true;
        this.title = "修改字典数据";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dictId != undefined) {
            updateType(this.form).then(response => {
              if(response.data.code == 200){
                this.$notify.success({
                  title: '成功',
                  message: "修改成功"
                })
              } else{
                this.$notify.error({
                  title: '失败',
                  message: response.data.msg
                })
              }
              this.open = false;
              this.getList();
            });
          } else {
            addType(this.form).then(response => {
              if(response.data.code == 200){
                this.$notify.success({
                  title: '成功',
                  message: "新增成功"
                })
              } else{
                this.$notify.error({
                  title: '失败',
                  message: response.data.msg
                })
              }

              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    dsubmitForm: function() {
      this.$refs["dform"].validate(valid => {
        if (valid) {
          if (this.dform.dictCode != undefined) {
            updateData(this.dform).then(response => {
              if(response.data.code == 200){
                this.$notify.success({
                  title: '成功',
                  message: "修改成功"
                })
              } else{
                this.$notify.error({
                  title: '失败',
                  message: response.data.msg
                })
              }
              this.dopen = false;
              this.getDataList();
            });
          } else {
            addData(this.dform).then(response => {
              if(response.data.code == 200){
                this.$notify.success({
                  title: '成功',
                  message: "新增成功"
                })
              } else{
                this.$notify.error({
                  title: '失败',
                  message: response.data.msg
                })
              }
              this.dopen = false;
              this.getDataList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictIds = row.dictId || this.ids;
      this.$confirm('是否确认删除字典编号为"' + dictIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
      }).then(function() {
        debugger
        return delType(dictIds);
      }).then((res) => {
        if(res.data.code == 200){
          this.getList();
          this.$notify.success({
            title: '成功',
            message: "删除成功"
          })
        }else{
          this.$notify.error({
            title: '成功',
            message: res.data.msg
          })
        }
      })
    },
    /** 删除按钮操作 */
    dhandleDelete(row) {
      const dictCodes = row.dictCode || this.dids;
      this.$confirm('是否确认删除字典编码为"' + dictCodes + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delData(dictCodes);
      }).then((res) => {
        this.getDataList();
        this.$notify.success({
          title: '成功',
          message: "删除成功"
        })
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有类型数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportType(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    /** 清理缓存按钮操作 */
    handleClearCache() {
      clearCache().then(response => {
        this.msgSuccess("清理成功");
      });
    }
  }
};
</script>
<style scoped lang="scss">
.uc-table{
  width: 100%;
  font-size: 14px;
}
.border-container{
  padding: 10px;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 5px;
}
.container {
  padding: 10px;
  background: #fff;
}

.other-box{
  margin-bottom: 10px;
}

.handle-box {
  .el-form{
    .el-form-item{
      .el-input{
        margin-right: 10px;
        width: 200px;
      }
    }
  }
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 200px;
  display: inline-block;
}
.el-table .success-row {
  background: #0bb20c;
}
</style>
